package com.example.a429;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class RecommendationAdapter extends RecyclerView.Adapter<RecommendationAdapter.ViewHolder> {
    private final List<RecommendationItem> recommendationList;

    public RecommendationAdapter(List<RecommendationItem> list) {
        this.recommendationList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RecommendationItem item = recommendationList.get(position);
        holder.imageView.setImageResource(item.getImageResource());
        holder.nameTextView.setText(item.getName());
        holder.priceTextView.setText("¥" + item.getPrice());
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), RecommendationDetailActivity.class);
            intent.putExtra("itemName", item.getName());
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return recommendationList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView nameTextView;
        TextView priceTextView;

        public ViewHolder(@NonNull View view) {
            super(view);
            imageView = view.findViewById(R.id.recommendation_image);
            nameTextView = view.findViewById(R.id.recommendation_name);
            priceTextView = view.findViewById(R.id.recommendation_price);
        }
    }
}